/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.jrwz.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.jrwz.server.ClientThread;
import l1j.jrwz.server.model.L1Object;
import l1j.jrwz.server.model.L1World;
import l1j.jrwz.server.model.Instance.L1PcInstance;
import l1j.jrwz.server.serverpackets.S_Message_YN;
import l1j.jrwz.server.serverpackets.S_ServerMessage;

// Referenced classes of package l1j.jrwz.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來建立組隊的封包
 */
public class C_CreateParty extends ClientBasePacket {

    private static final String C_CREATE_PARTY = "[C] C_CreateParty";

    private static Logger _log = Logger
            .getLogger(C_CreateParty.class.getName());

    @Override
    public void execute(byte[] decrypt, ClientThread client) {
        try {
            read(decrypt);

            L1PcInstance pc = client.getActiveChar();
            if (pc == null) {
                return;
            }
            if (pc.isDead()) {
                return;
            }
            if (pc.isGhost()) {
                return;
            }

            int type = readC();
            if (type == 0 || type == 1) { // 自動接受組隊 on 與 off 的同
                int targetId = readD();
                L1Object temp = L1World.getInstance().findObject(targetId);
                if (temp instanceof L1PcInstance) {
                    L1PcInstance targetPc = (L1PcInstance) temp;
                    if (pc.getId() == targetPc.getId()) {
                        return;
                    }
                    // 邀请组队时，对象不再荧幕内或是7步内
                    if ((!pc.getLocation().isInScreen(targetPc.getLocation()) || (pc
                            .getLocation().getTileLineDistance(
                                    targetPc.getLocation()) > 7))) {
                        pc.sendPackets(new S_ServerMessage(952)); // 距离太远以致于无法邀请至队伍。
                        return;
                    }
                    if (targetPc.isInParty()) {
                        // 您無法邀請已經參加其他隊伍的人。
                        pc.sendPackets(new S_ServerMessage(415));
                        return;
                    }

                    if (pc.isInParty()) {
                        if (pc.getParty().isLeader(pc)) {
                            targetPc.setPartyID(pc.getId());
                            // 玩家 %0%s 邀請您加入隊伍？(Y/N)
                            targetPc.sendPackets(new S_Message_YN(953, pc
                                    .getName()));
                        } else {
                            // 只有領導者才能邀請其他的成員。
                            pc.sendPackets(new S_ServerMessage(416));
                        }
                    } else {
                        pc.setPartyType(type);
                        targetPc.setPartyID(pc.getId());
                        switch (type) {
                            case 0:
                                // 玩家 %0%s 邀請您加入隊伍？(Y/N)
                                targetPc.sendPackets(new S_Message_YN(953, pc
                                        .getName()));
                                break;
                            case 1:
                                // \f2%0\f>%s 邀请\fU自动分配队伍\f>。接受吗？(Y/N)
                                targetPc.sendPackets(new S_Message_YN(954, pc
                                        .getName()));
                                break;
                        }
                    }
                }
            } else if (type == 2) { // 聊天組隊
                String name = readS();
                L1PcInstance targetPc = L1World.getInstance().getPlayer(name);
                if (targetPc == null) {
                    // 沒有叫%0的人。
                    pc.sendPackets(new S_ServerMessage(109));
                    return;
                }
                if (pc.getId() == targetPc.getId()) {
                    return;
                }
                // 邀请组队时，对象不再荧幕内或是7步内
                if ((!pc.getLocation().isInScreen(targetPc.getLocation()) || (pc
                        .getLocation().getTileLineDistance(
                                targetPc.getLocation()) > 7))) {
                    pc.sendPackets(new S_ServerMessage(952)); // 距离太远以致于无法邀请至队伍。
                    return;
                }
                if (targetPc.isInChatParty()) {
                    // 您無法邀請已經參加其他隊伍的人。
                    pc.sendPackets(new S_ServerMessage(415));
                    return;
                }

                if (pc.isInChatParty()) {
                    if (pc.getChatParty().isLeader(pc)) {
                        targetPc.setPartyID(pc.getId());
                        // 您要接受玩家 %0%s 提出的隊伍對話邀請嗎？(Y/N)
                        targetPc.sendPackets(new S_Message_YN(951, pc.getName()));
                    } else {
                        // 只有領導者才能邀請其他的成員。
                        pc.sendPackets(new S_ServerMessage(416));
                    }
                } else {
                    targetPc.setPartyID(pc.getId());
                    // 您要接受玩家 %0%s 提出的隊伍對話邀請嗎？(Y/N)
                    targetPc.sendPackets(new S_Message_YN(951, pc.getName()));
                }
            } else if (type == 3) { // 队长委任
                // 不是队长时, 不可使用
                if ((pc.getParty() == null) || !pc.getParty().isLeader(pc)) {
                    pc.sendPackets(new S_ServerMessage(1697)); // 不是队长无法执行该操作。
                    return;
                }
                // 取得目标物件编号
                int targetId = readD();
                // 尝试取得目标
                L1Object obj = L1World.getInstance().findObject(targetId);
                // 判断目标是否合理
                if ((obj == null) || (pc.getId() == obj.getId())
                        || !(obj instanceof L1PcInstance)) {
                    return;
                }
                // 邀请组队时，对象不再荧幕内或是7步内
                if ((!pc.getLocation().isInScreen(obj.getLocation()) || (pc
                        .getLocation().getTileLineDistance(obj.getLocation()) > 7))) {
                    pc.sendPackets(new S_ServerMessage(1695)); // 需要委任的队长不在附近。
                    return;
                }
                // 转型为玩家物件
                L1PcInstance targetPc = (L1PcInstance) obj;
                // 判断目标是否属于相同队伍
                if (!targetPc.isInParty()) {
                    pc.sendPackets(new S_ServerMessage(1696)); // 不是目前队伍的成员。
                    return;
                }
                pc.sendPackets(new S_Message_YN(1703, "")); // 需要委任队长吗？
                // 指定队长给新的目标
                pc.getParty().passLeader(targetPc);
            }
        } catch (final Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            finish();
        }
    }

    @Override
    public String getType() {
        return C_CREATE_PARTY;
    }

}
